package com.sicau.smarttraceplatform.controller;


import com.sicau.smarttraceplatform.common.aop.ValidateAspect;
import com.sicau.smarttraceplatform.common.returning.ResultData;
import com.sicau.smarttraceplatform.entity.CollectingData;
import com.sicau.smarttraceplatform.entity.User;
import com.sicau.smarttraceplatform.entity.param.collectingdata.CollectingDataAddParam;
import com.sicau.smarttraceplatform.entity.param.collectingdata.CollectingDataQueryParam;
import com.sicau.smarttraceplatform.entity.vo.collectingdata.CollectingDataVO;
import com.sicau.smarttraceplatform.service.CollectingDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;

import static com.sicau.smarttraceplatform.common.constants.CommonURLConstants.COLLECTING_DATA_PREFIX;
import static com.sicau.smarttraceplatform.common.constants.commonConstants.CURRENT_USER;

/**
 * <p>
 * 采集数据 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2020-05-24
 */
@RestController
@RequestMapping(COLLECTING_DATA_PREFIX)
@Api(tags = "采集数据")
@Slf4j
public class CollectingDataController {

    @Resource
    private CollectingDataService collectingDataService;


    @PostMapping
    @ApiOperation(value = "新增采集数据记录")
    @ValidateAspect.ParamValidate()
    public ResultData<Boolean> addCollectingDataInfo(@ApiParam(hidden = true) HttpServletRequest request,
                                                     @Valid CollectingDataAddParam collectingDataAddParam,
                                                     @ApiParam(hidden = true) @SuppressWarnings("unused") BindingResult bindingResult) {

        //数据整理
        CollectingData collectingData = new CollectingData();
        BeanUtils.copyProperties(collectingDataAddParam, collectingData);
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        collectingData.setCreator(user != null ? user.getId().toString() : null)
                .setCreated(LocalDateTime.now());
        log.info("新增采集数据记录操作, 采集数据信息: {}", collectingData);

        //添加操作
        boolean rs = collectingDataService.save(collectingData);

        //结果返回
        if (rs) {
            log.info("采集数据添加成功");
            return new ResultData<Boolean>()
                    .OK()
                    .setResultIns("采集数据添加成功")
                    .setObj(Boolean.TRUE);
        } else {
            log.info("采集数据添加失败");
            return new ResultData<Boolean>()
                    .FAILED()
                    .setResultIns("采集数据添加失败")
                    .setObj(Boolean.FALSE);
        }

    }


    @GetMapping
    @ApiOperation(value = "查询采集数据记录(传入条件则根据条件查询, 否则查询全部数据)")
    @ValidateAspect.ParamValidate()
    public ResultData<CollectingDataVO> getCollectingDataInfos(@Valid CollectingDataQueryParam queryParam,
                                                               @ApiParam(hidden = true) @SuppressWarnings("unused") BindingResult bindingResult) {
        return new ResultData<CollectingDataVO>()
                .OK()
                .setObj(collectingDataService.queryCollectingDataInfosByCondition(queryParam));
    }

    @DeleteMapping
    @ApiOperation(value = "根据id删除采集数据记录")
    public ResultData<Boolean> removeTraceInfo(@RequestParam Integer id) {
        log.info("根据id删除采集数据记录, id: {}", id);
        boolean rs = collectingDataService.removeById(id);
        log.info("删除结果: {}", rs ? "删除成功" : "删除失败");
        return new ResultData<Boolean>().OK().setObj(rs).setResultIns(rs ? "删除成功" : "删除失败, 或者该条记录是否已经被删除?");
    }


}

